package com.javabasic.algorithm.sword_of_offer;

import org.junit.Test;

/**
 * @Author xiongmin
 * @ClassName 替换空格
 * @Description TODO
 * @Date 2019/9/7 21:53
 * @Version 1.0
 **/
public class 替换空格 {

    @Test
    public void main() {
        String result = replaceSpace(new StringBuffer("We Are Happy"));
        System.out.println(result);
    }

    public String replaceSpace(StringBuffer str) {
        int len = str.length();
        StringBuilder result = new StringBuilder("");
        for (int i = 0; i < len; i++) {
            if (str.charAt(i) == ' ') {
                result.append("%20");
            } else {
                result.append(str.charAt(i));
            }
        }
        return result.toString();
    }
}
